<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en-us" lang="en-us">
<head>
  <link href="//gmpg.org/xfn/11" rel="profile">
  <meta http-equiv="content-type" content="text/html; charset=utf-8">
  <meta name="generator" content="Hugo 0.69.0" />

  
  <meta name="viewport" content="width=device-width, initial-scale=1.0">

  <title>PIL图片添加文字 &middot; 郝朝帅的博客</title>

  
  <link type="text/css" rel="stylesheet" href="/blog/css/print.css" media="print">
  <link type="text/css" rel="stylesheet" href="/blog/css/poole.css">
  <link type="text/css" rel="stylesheet" href="/blog/css/syntax.css">
  <link type="text/css" rel="stylesheet" href="/blog/css/hyde.css">
    <link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Abril+Fatface|PT+Sans:400,400i,700">


  
  <link rel="apple-touch-icon-precomposed" sizes="144x144" href="/apple-touch-icon-144-precomposed.png">
  <link rel="shortcut icon" href="/favicon.png">

  
  
</head>

  <body class="theme-base-0f ">
  <aside class="sidebar">
  <div class="container sidebar-sticky">
    <div class="sidebar-about">
      <a href="/blog/"><h1>郝朝帅的博客</h1></a>
      <p class="lead">
       我的个人博客 
      </p>
    </div>

    <nav>
      <ul class="sidebar-nav">
        <li><a href="/blog/">Home</a> </li>
        
      </ul>
    </nav>

    <p>&copy; 2020. All rights reserved. </p>
  </div>
</aside>

    <main class="content container">
    <div class="post">
  <h1>PIL图片添加文字</h1>
  <time datetime=2020-04-29T19:01:49&#43;0800 class="post-date">Wed, Apr 29, 2020</time>
  <p>图片添加文字</p>
<p>安装PLI第三方库
pip install PIL</p>
<div class="highlight"><pre style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-python" data-lang="python"><span style="color:#75715e"># 导入PIL</span>
<span style="color:#f92672">from</span> PIL <span style="color:#f92672">import</span> Image,ImageDraw,ImageFont

<span style="color:#75715e">#读图</span>
im <span style="color:#f92672">=</span> Image<span style="color:#f92672">.</span>open(<span style="color:#e6db74">&#34;./timg.jfif&#34;</span>)

<span style="color:#75715e"># 输出（格式信息，图片大小，模式属性）</span>
<span style="color:#66d9ef">print</span>(im<span style="color:#f92672">.</span>format,im<span style="color:#f92672">.</span>size,im<span style="color:#f92672">.</span>mode)

<span style="color:#75715e">#生成画笔</span>
draw <span style="color:#f92672">=</span> ImageDraw<span style="color:#f92672">.</span>Draw(im)

<span style="color:#75715e"># 字体</span>
font<span style="color:#f92672">=</span>ImageFont<span style="color:#f92672">.</span>truetype(<span style="color:#e6db74">&#34;simsun.ttc&#34;</span>, <span style="color:#ae81ff">40</span>, encoding<span style="color:#f92672">=</span><span style="color:#e6db74">&#34;GBK&#34;</span>)
<span style="color:#75715e">#绘制</span>
draw<span style="color:#f92672">.</span>text((<span style="color:#ae81ff">1700</span>, <span style="color:#ae81ff">1317</span>),<span style="color:#e6db74">u</span><span style="color:#e6db74">&#39;车型：COVINI C6W </span><span style="color:#ae81ff">\n</span><span style="color:#e6db74">作者：郝朝帅&#39;</span>,<span style="color:#e6db74">&#39;#00FFFF&#39;</span>,font)
<span style="color:#75715e"># draw.font((2026, 1317),&#39;COVINI C6W郝朝帅&#39;,fill=(220,20,60))</span>

<span style="color:#75715e"># 保存图片</span>
im<span style="color:#f92672">.</span>save(<span style="color:#e6db74">&#39;timg.jfif&#39;</span>)

<span style="color:#75715e"># 删除画笔</span>
<span style="color:#66d9ef">del</span> draw

<span style="color:#75715e"># 打开，并查看图片</span>
im<span style="color:#f92672">.</span>show()


</code></pre></div>
</div>


    </main>

    
  </body>
</html>
